//
//  GFMakeDetialsViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFMakeDetialsViewController.h"

@interface GFMakeDetialsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;


@property (nonatomic, strong) NSDictionary *mainDic;
@property (nonatomic, strong) NSMutableArray *condiArr;
@property (nonatomic, strong) NSMutableArray *stepArr;
@property (nonatomic, strong) GFRecipeCondiModel *condiModel;
@property (nonatomic, strong) GFRecipeStepModel *stepModel;
@end

@implementation GFMakeDetialsViewController

#pragma mark _ 创建数据
- (void)handleData {
    
    NSString *urlStr = [self.frontUrlStr stringByAppendingString:@"/recipe"];
    
    [AFNetHandle GETWithURL:urlStr body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.mainDic = [NSDictionary dictionaryWithDictionary:result];
        self.condiArr = [NSMutableArray array];
        self.stepArr = [NSMutableArray array];
        
        
        NSArray *arr1 = [self.mainDic objectForKey:@"condiments"];
        NSArray *arr2 = [self.mainDic objectForKey:@"steps"];
        
        for (NSDictionary *dic in arr1) {
            self.condiModel = [GFRecipeCondiModel modWithDic:dic];
            
            [self.condiArr addObject:self.condiModel];
        }
        
        for (NSDictionary *dic in arr2) {
            self.stepModel = [GFRecipeStepModel modWithDic:dic];
            
            [self.stepArr addObject:self.stepModel];
        }
        
        [self.tableView reloadData];
        [self.overTimeView removeFromSuperview];
        [self.waitView removeFromSuperview];
        
        
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
    }];
}



#pragma mark _ 创建tableView
- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    
    /* 重用池 */
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.tableView registerClass:[TitleImagCell class] forCellReuseIdentifier:@"TitleImagCell"];
    [self.tableView registerClass:[UseFoodCell class] forCellReuseIdentifier:@"UseFoodCell"];
    [self.tableView registerClass:[StepCell class] forCellReuseIdentifier:@"StepCell"];

}



#pragma mark _ tableView DataSource 的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1) {
        
        return self.condiArr.count;
    } else if (section == 2) {
        
        return self.stepArr.count;
    } else {
        
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        TitleImagCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleImagCell"];
        cell.imageName = [self.mainDic objectForKey:@"image_url"];
        
        return cell;
        
    } else if (indexPath.section == 1) {
        
        UseFoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UseFoodCell"];
        cell.condiModel = [self.condiArr objectAtIndex:indexPath.row];
        
        return cell;
    } else if (indexPath.section == 2) {
        
        
        StepCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StepCell"];
        cell.stepModel = [self.stepArr objectAtIndex:indexPath.row];
        
        return cell;
    } else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        cell.textLabel.text = [self.mainDic objectForKey:@"tips"];
        cell.textLabel.numberOfLines = 0;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        
        return cell;
    }
}

/* 分区数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

/* headView高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0;
    } else {
        return ScreenWidth / 375.0 * 50;
    }
}

/* headView内容 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
        
        view.backgroundColor = [UIColor colorWithRed:237 / 255.0 green:237 / 255.0 blue:237 / 255.0 alpha:1];
        
        GFBaseLabel *label = [[GFBaseLabel alloc] initWithFrame:CGRectMake(ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 20)];
        
        label.text = @"原料";
        [view addSubview:label];
        
        return view;
        
    } else if (section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 50)];
        
        view.backgroundColor = [UIColor colorWithRed:237 / 255.0 green:237 / 255.0 blue:237 / 255.0 alpha:1];
        
        GFBaseLabel *label = [[GFBaseLabel alloc] initWithFrame:CGRectMake(ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 20)];
        
        label.text = @"做法";
        [view addSubview:label];
        
        
        return view;
        
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 50)];
        
        view.backgroundColor = [UIColor colorWithRed:237 / 255.0 green:237 / 255.0 blue:237 / 255.0 alpha:1];
        
        GFBaseLabel *label = [[GFBaseLabel alloc] initWithFrame:CGRectMake(ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 20)];
        
        label.text = @"小贴士";
        [view addSubview:label];
  
        return view;
    }
}

/* 行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return ScreenWidth / 375.0 * 250;
    } else if (indexPath.section == 1) {
        return ScreenWidth / 375.0 * 50;
    } else if (indexPath.section == 2) {
        
        GFRecipeStepModel *stepModel = [self.stepArr objectAtIndex:indexPath.row];
        NSString *text = stepModel.desc;
        
        if (self.stepModel.image_url == nil) {
            CGFloat h = [GFCalculateWidthOrHeigth stringHeight:text withWidth:ScreenWidth / 375.0 * 285 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
            if (h < ScreenWidth / 375.0 * 150) {
                return ScreenWidth / 375.0 * 170;
            } else {
                return h + ScreenWidth / 375.0 * 40;
            }
        } else {
            CGFloat h = [GFCalculateWidthOrHeigth stringHeight:text withWidth:ScreenWidth / 375.0 * 130 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
            if (h < ScreenWidth / 375.0 * 150) {
                return ScreenWidth / 375.0 * 170;
            } else {
                return h + ScreenWidth / 375.0 * 40;
            }
        }
    } else {
        
        CGFloat h = [GFCalculateWidthOrHeigth stringHeight:[self.mainDic objectForKey:@"tips"] withWidth:ScreenWidth / 375.0 * 335 withFontDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
        
        return h + ScreenWidth / 375.0 * 40;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatTableView];
    [self.view bringSubviewToFront:self.waitView];
    
    [self handleData];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
